package cn.edu.ctbu.scoremg.api;

import cn.edu.ctbu.scoremg.constant.REnum;
import cn.edu.ctbu.scoremg.entity.Course;
import cn.edu.ctbu.scoremg.exception.RException;
import cn.edu.ctbu.scoremg.service.CourseService;
import cn.edu.ctbu.scoremg.util.RUtil;
import cn.edu.ctbu.scoremg.vo.QueryObj;
import cn.edu.ctbu.scoremg.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/api/course")
@Slf4j
public class CourseApiController {
    @Autowired
    private CourseService courseService;

    /**
     * 查所有课程信息
     * @return
     */
    @GetMapping("/list")
    public R<List<Course>> findAll(){
        List<Course> courses = courseService.getAll();
        return RUtil.success(courses);
    }

    /**
     * 新增
     * @param course
     * @return
     */
    @PostMapping("/add")
    public R<Course> add(Course course) {
        return RUtil.success(courseService.add(course));
    }

    /**
     * 查，id
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Course> findById(@PathVariable Integer id) {
        return RUtil.success(courseService.findById(id));
    }

    /**
     * 修改
     * @param course
     * @return
     */
    @PutMapping("/update")
    public R<Course> update(Course course) {
        return RUtil.success(courseService.update(course));
    }

    /**
     * 删
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        courseService.delete(id);
        return RUtil.success();
    }

    /**
     * 删多个
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteId/{ids}")
    public R deleteByIds(@PathVariable String ids) {
        try {
            List<Integer> courseIds = Stream.of(ids.split(","))
                    .map(Integer::parseInt).collect(Collectors.toList());
            courseService.deleteByIds(courseIds);
        } catch (Exception e) {
            return RUtil.error(1, e.getMessage());
        }
        return RUtil.success();
    }

    /**
     * 分页
     * @param qObj
     * @return
     */
    @PostMapping("/getbypage")
    public R<Page<Course>> getByPage(@RequestBody QueryObj<Course> qObj) {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Integer pageIndex =0;
        Integer pageSize =10;
        if (qObj==null){
            Pageable pageable= PageRequest.of(pageIndex,pageSize,sort);
            Page<Course> courses = courseService.getAll(pageable);
            return RUtil.success(courses.getContent(),courses.getTotalElements());
        }else {
            pageIndex=qObj.getPage()-1;
            pageSize=qObj.getLimit();
            if (qObj.getData() instanceof Course){
                Course course = (Course) qObj.getData();
                Pageable pageable= PageRequest.of(pageIndex,pageSize,sort);
                ExampleMatcher matcher=ExampleMatcher.matching()
                        .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();
                Example<Course> example=Example.of(course,matcher);
                Page<Course> coursePage = courseService.getAll(example,pageable);
                return RUtil.success(coursePage.getContent(),coursePage.getTotalElements());
            }else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}
